﻿using Common;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZoneLine.Models;
using ZoneLine.Models.PackageModel;
using ZoneLine.Models.StationConfig;

namespace ZoneLine.ViewModels
{

    public class FormulaConfigViewModel : ObservableObject
    {

        private readonly IModelDAL _dal;

        public FormulaConfigViewModel(IModelDAL modelDAL)
        {
            this._dal = modelDAL;
            FormulaConfigModel = new FormulaConfigModel();
            //GetFormulaConfigModelList();
            GetStationList();
        }


        private FormulaConfigModel formulaConfigModel;
        /// <summary>
        /// 配方
        /// </summary>
        public FormulaConfigModel FormulaConfigModel
        {
            get { return formulaConfigModel; }
            set { formulaConfigModel = value; OnPropertyChanged(); }
        }


        private FormulaConfigModel selectedItem;
        /// <summary>
        /// 列表
        /// </summary>
        public FormulaConfigModel SelectedItem
        {
            get { return selectedItem; }
            set { selectedItem = value; OnPropertyChanged(); }
        }


        private ObservableCollection<StationConfigBase> stationConfigList;
        /// <summary>
        /// 工站列表
        /// </summary>
        public ObservableCollection<StationConfigBase> StationConfigList
        {
            get { return stationConfigList; }
            set { stationConfigList = value; OnPropertyChanged(); }
        }

        private ObservableCollection<FormulaConfigModel> formulaConfigModelList;
        /// <summary>
        /// 配方
        /// </summary>
        public ObservableCollection<FormulaConfigModel> FormulaConfigModelList
        {
            get { return formulaConfigModelList; }
            set { formulaConfigModelList = value;OnPropertyChanged(); }
        }


        public RelayCommand SaveCmd => new RelayCommand(() => {
            if (FormulaConfigModel != null)
            {
                if (string.IsNullOrEmpty( FormulaConfigModel.PlcIp))
                {
                    HandyControl.Controls.MessageBox.Error("工位设备IP不能为空！", "温馨提示");
                    return;
                }

                if (!FormulaConfigModel.Number.HasValue)
                {
                    HandyControl.Controls.MessageBox.Error("工位设备配方编号不能为空！", "温馨提示");
                    return;
                }

                if (string.IsNullOrEmpty(FormulaConfigModel.FieldDescription))
                {
                    HandyControl.Controls.MessageBox.Error("工位设备字段描述不能为空！", "温馨提示");
                    return;
                }

                if (string.IsNullOrEmpty(FormulaConfigModel.FieldValue))
                {
                    HandyControl.Controls.MessageBox.Error("工位设备字段值不能为空！", "温馨提示");
                    return;
                }

                FormulaConfigModel.CreateTime = DateTime.Now;
                var res = _dal.AddModel(FormulaConfigModel);
                if (res)
                {
                    //HandyControl.Controls.Growl.Success("添加成功！", "温馨提示");
                    GetFormulaConfigModelList();
                }
            }
        });

        public RelayCommand<FormulaConfigModel> DeleteCmd => new RelayCommand<FormulaConfigModel>((obj) => {
            if (obj != null)
            {
                var res = HandyControl.Controls.MessageBox.Ask("是否确定删除当前配方的数据？", "温馨提示");
                if (res == MessageBoxResult.OK)
                {
                    var rls = _dal.DeleteModel<FormulaConfigModel>(o => o.ID == obj.ID);
                    if (rls)
                    {
                        //HandyControl.Controls.Growl.Success("删除成功！", "温馨提示");
                        GetFormulaConfigModelList();
                    }
                }
            }
        });

        public RelayCommand<StationConfigBase> StationConfigSelectedCmd => new RelayCommand<StationConfigBase>((obj) =>
        {
            if (obj != null)
            {
                GetFormulaConfigModelList(obj.Ip);
            }
        });
        


        private void GetFormulaConfigModelList(string ip = "")
        {
            var list = new List<FormulaConfigModel>();
            if (ip != "")
            {
                list = _dal.GetModelList<FormulaConfigModel>(o => o.PlcIp == ip);
            }
            else
            {
                list = _dal.GetModelList<FormulaConfigModel>(o => true);
            }

            FormulaConfigModelList = new ObservableCollection<FormulaConfigModel>(list.ToList());
        }

        private void GetStationList()
        {
            StationConfigList = new ObservableCollection<StationConfigBase>();
            if (Cache.IsQopStaion)
            {
                var obj3 = _dal.GetModelList<QOP006StationConfig>(o => 1 == 1).FirstOrDefault();
                var obj4 = _dal.GetModelList<QOP010StationConfig>(o => 1 == 1).FirstOrDefault();
                var obj5 = _dal.GetModelList<QOP020StationConfig>(o => 1 == 1).FirstOrDefault();
                var obj6 = _dal.GetModelList<QOP050LStationConfig>(o => 1 == 1).FirstOrDefault();
                var obj7 = _dal.GetModelList<QOP050RStationConfig>(o => 1 == 1).FirstOrDefault();
                //StationConfigList.Add(obj3);
                StationConfigList.Add(obj5);
                StationConfigList.Add(obj4);
                StationConfigList.Add(obj6);
                StationConfigList.Add(obj7);
            }
            else if (Cache.IsHopStaion)
            {
                var obj1 = _dal.GetModelList<HOP010StationConfig>(o => 1 == 1).FirstOrDefault();
                var obj2 = _dal.GetModelList<HOP040StationConfig>(o => 1 == 1).FirstOrDefault();
                StationConfigList.Add(obj1);
                StationConfigList.Add(obj2);
            }
        }


        

    }


}
